//
//  ListFlat.m
//  FinApp
//
//  Created by Victor Leandro Fernandez Rodrigo on 11/11/11.
//  Copyright (c) 2011 FIB-Fàcultat d'informàtica de Barcelona. All rights reserved.
//

#import "ListFlat.h"
#import "LocationManager.h"

@implementation ListFlat

@synthesize idFlat = _idFlat;
@synthesize street = _street;
@synthesize city = _city;
@synthesize flatType = _flatType;
@synthesize dimensions = _dimensions;
@synthesize numRooms = _numRooms;
@synthesize price = _price;
@synthesize imageURL = _imageURL;
@synthesize latitude = _latitude;
@synthesize longitude = _longitude;
@synthesize distance = _distance;
@synthesize lowerPrice = _lowerPrice;

- (void) dealloc {
    [_street release];
    [_city release];
    [_imageURL release];
    [_flatType release];
    [super dealloc];
}


+ (ListFlat*)listFlatFromDictionary:(NSDictionary*)flatDictionary {
    ListFlat *newListFlat = [[[ListFlat alloc] init] autorelease];
    [newListFlat setIdFlat:[[flatDictionary objectForKey:@"idPis"] intValue]];
    [newListFlat setStreet:[flatDictionary objectForKey:@"carrer"]];
    [newListFlat setCity:[flatDictionary objectForKey:@"poblacio"]];
    [newListFlat setFlatType:[flatDictionary objectForKey:@"tipus"]];
    [newListFlat setDimensions:[[flatDictionary objectForKey:@"superficie"] intValue]];
    [newListFlat setNumRooms:[[flatDictionary objectForKey:@"numHabitacions"] intValue]];
     [newListFlat setPrice:[[flatDictionary objectForKey:@"preu"] floatValue]];
     [newListFlat setImageURL:[flatDictionary objectForKey:@"imageURL"]];
     [newListFlat setLatitude:[[flatDictionary objectForKey:@"latitut"] floatValue]];
     [newListFlat setLongitude:[[flatDictionary objectForKey:@"longitut"] floatValue]];
     [newListFlat setLowerPrice:[[flatDictionary objectForKey:@"haBaixat"] boolValue]];
     
     CLLocation *newLocation = [[CLLocation alloc] initWithLatitude:newListFlat.latitude longitude:newListFlat.longitude];
    [newListFlat setDistance:[[[LocationManager sharedInstance] myLocation] distanceFromLocation:newLocation]];
     [newLocation release];
     return newListFlat;
}

+ (NSDictionary*)dictionaryFromFlat:(ListFlat*)listFlat {
    
    NSArray *keysArray = [[NSArray alloc] initWithObjects:@"idPis",@"carrer",@"poblacio",@"tipus",@"superficie",@"numHabitacions",@"preu",@"imageURL",@"latitut",@"longitut", nil];
    NSArray *objectsArray = [[NSArray alloc] initWithObjects:
                             [NSNumber numberWithInt:[listFlat idFlat]],
                             [listFlat street],
                             [listFlat city],
                             [listFlat flatType],
                             [NSNumber numberWithInt:[listFlat dimensions]],
                             [NSNumber numberWithInt:[listFlat numRooms]],
                             [NSNumber numberWithFloat:[listFlat price]],
                             [listFlat imageURL],
                             [NSNumber numberWithFloat:[listFlat latitude]],
                             [NSNumber numberWithFloat:[listFlat longitude]],
                             nil];
    
    NSDictionary *newDict = [[NSDictionary alloc] initWithObjects:objectsArray forKeys:keysArray];
    
    return newDict;
}

@end
